/*~-1*/
/*~XSF_LANGUAGE: C/C++*/
/*~A:1*/
/*~+:File Header*/
/*~T*/
/****************************************************************************
 * 
 ****************************************************************************/

/*~E:A1*/
/*~A:2*/
/*~+:Import*/
/*~T*/
/* Import                                                                      */

/*~T*/
/* generic definitions */
#include "general.h"

/*~T*/
#include "init.h"
#include "actuator.h"
#include "line.h"
#include "bsl_com.h"
#include <avr/io.h>
#include <avr/pgmspace.h> 

/*~T*/
/* own header file */
#include "main.h"

/*~T*/

/*~E:A2*/
/*~A:3*/
/*~+:Configuration*/
/*~T*/

/*~E:A3*/
/*~A:4*/
/*~+:Private Definitions           */
/*~A:5*/
/*~+:Private Constant Definitions */
/*~T*/
/* Private Variable Definitions                                                */

/*~T*/
static const u8 bsl_ID_code[1] PROGMEM= {0x00};
static const u8 bsl_code_1[32] PROGMEM= {0xe7,0x58,0xa6,0x00,0xe6,0xf0,0x5f,0xfa,
                                  0x7e,0xb7,0x9a,0xb7,0xfe,0x70,0xa4,0x00,
                                  0xb2,0xfe,0x86,0xf0,0x8f,0xfa,0x3d,0xf8,
                                  0xea,0x00,0x60,0xfa,0xcc,0x00,0xcc,0x00};
static const u8 bsl_code_2[48] PROGMEM = {0xE7,0x58,0xA6,0x00,0x5F,0xEB,0xE6,0xF0,
                                  0xFF,0xBF,0x7E,0xB7,0x5F,0xEA,0x5E,0xEA,
                                  0x9A,0xB7,0xFC,0x70,0xA4,0x00,0xB2,0xFE,
                                  0x86,0xF0,0x2f,0xc4,0x3D,0xF6,0xEA,0x00,
                                  0x00,0xc0,0xCC,0x00,0xCC,0x00,0xCC,0x00,
                                  0xCC,0x00,0xCC,0x00,0xCC,0x00,0xCC,0x00};
static const u8 bsl_code_3[48] PROGMEM = {0xE7,0x58,0xA6,0x00,0x5F,0xEB,0xE6,0xF0,
                                  0x0B,0xF2,0x7E,0xB7,0x5F,0xEA,0x5E,0xEA,
                                  0x9A,0xB7,0xFC,0x70,0xA4,0x00,0xB2,0xFE,
                                  0x86,0xF0,0xFF,0xF5,0x3D,0xF6,0xEA,0x00,
                                  0x14,0xF2,0xCC,0x00,0xCC,0x00,0xCC,0x00,
                                  0xCC,0x00,0xCC,0x00,0xCC,0x00,0xCC,0x00};

/*~E:A5*/
/*~A:6*/
/*~+:Private Variable Definitions */
/*~T*/
/* Private Variable Definitions                                                */

/*~T*/
static u8 lv_boot_en;
static u8 lv_bat_elec;

/*~E:A6*/
/*~T*/

/*~E:A4*/
/*~A:7*/
/*~+:Private Functions*/
/*~T*/
/* Private Functions                                                           */

/*~F:8*/
void perform_init()
/*~-1*/
{
   /*~T*/
   u8 lv_prg_start=0;

   /*~A:9*/
   /*~+:start the programming mode*/
   /*~I:10*/
   delay_ms(67);
   if (com_send(bsl_ID_code, 1, 0xD5) == COM_OK)
   /*~-1*/
   {
      /*~T*/
      delay_ms(1);

      /*~I:11*/
      if (com_send(bsl_code_1, 32, 0xA6) == COM_OK)
      /*~-1*/
      {
         /*~T*/
         delay_ms(2);

         /*~I:12*/
         if (com_send(bsl_code_3, 48, 0xA6) == COM_OK)
         /*~-1*/
         {
            /*~T*/
            /* ready for DTS communication */

         /*~-1*/
         }
         /*~O:I12*/
         /*~-2*/
         else
         {
            /*~T*/
            /* BSL step 2 error */

         /*~-1*/
         }
         /*~E:I12*/
      /*~-1*/
      }
      /*~O:I11*/
      /*~-2*/
      else
      {
         /*~T*/
         /* BSL step 1 error */

      /*~-1*/
      }
      /*~E:I11*/
   /*~-1*/
   }
   /*~O:I10*/
   /*~-2*/
   else
   {
      /*~T*/
      /* ID error */

   /*~-1*/
   }
   /*~E:I10*/
   /*~T*/

   /*~E:A9*/
   /*~A:13*/
   /*~+:wait for the power supply line to be inactive*/
   /*~L:14*/
   /* wait until supply of the TCU goes off */
   while (line_status(DO0))
   /*~-1*/
   {
      /*~T*/

   /*~-1*/
   }
   /*~E:L14*/
   /*~T*/
   /* leave the BSL programming mode */
   lv_prg_start = 0;

   /*~T*/

   /*~E:A13*/
/*~-1*/
}
/*~E:F8*/
/*~E:A7*/
/*~A:15*/
/*~+:Main project entry*/
/*~F:16*/
void main(void)
/*~-1*/
{
   /*~T*/
   /* init IO line directions */
   io_init();

   /*~T*/
   /* init the input line read module */
   line_init();

   /*~T*/
   /* init the button read module */
   com_init();

   /*~T*/
   /* init the actuator control module */
   act_init();

   /*~A:17*/
   /*~+:main loop*/
   /*~T*/

   /*~L:18*/
   while (1)
   /*~-1*/
   {
      /*~A:19*/
      /*~+:check DO1 status*/
      /*~I:20*/
      if (line_status(DO1))
      /*~-1*/
      {
         /*~T*/
         act_set(BOOT_EN,1);
         act_set(LED_1,1);
         lv_boot_en=1;

      /*~-1*/
      }
      /*~O:I20*/
      /*~-2*/
      else
      {
         /*~I:21*/
         act_set(LED_1,0);
         if (lv_boot_en==1)
         /*~-1*/
         {
            /*~T*/
            act_set(BOOT_EN,0);
            lv_boot_en=0;

         /*~-1*/
         }
         /*~E:I21*/
      /*~-1*/
      }
      /*~E:I20*/
      /*~E:A19*/
      /*~A:22*/
      /*~+:check DO0 status*/
      /*~I:23*/
      if (line_status(DO0))
      /*~-1*/
      {
         /*~T*/
         act_set(BAT_ELEC,1);
         act_set(LED_2,1);
         lv_bat_elec=1;

         /*~I:24*/
         if (lv_boot_en==1)
         /*~-1*/
         {
            /*~T*/
            perform_init();
         /*~-1*/
         }
         /*~E:I24*/
      /*~-1*/
      }
      /*~O:I23*/
      /*~-2*/
      else
      {
         /*~I:25*/
         if (lv_bat_elec==1)
         /*~-1*/
         {
            /*~T*/
            act_set(BAT_ELEC,0);
            act_set(LED_2,0);
            lv_bat_elec=0;

         /*~-1*/
         }
         /*~E:I25*/
      /*~-1*/
      }
      /*~E:I23*/
      /*~E:A22*/
   /*~-1*/
   }
   /*~E:L18*/
   /*~T*/

   /*~T*/

   /*~E:A17*/
   /*~T*/

/*~-1*/
}
/*~E:F16*/
/*~E:A15*/
/*~T*/

